package com.project.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.project.bean.ContractBean;
import com.project.bean.PageBean;
import com.project.service.ICoachService;
import com.project.service.IContractService;

@Controller
public class ContractController {
	
    @Autowired
	private IContractService contractService;
    @Autowired
    private ICoachService coachService;
    
    public String addContract(HttpSession session,int ctUId2) {
    	ContractBean contract = new ContractBean();
    	contract.setCtUId1((int)session.getAttribute("UId"));
    	contract.setCtUId2(ctUId2);
    	contract.setCtState(1);
    	contractService.addContract(contract);
		return null;
    }
    
    @RequestMapping(value = "/coach_contract")
    public String coachContract(HttpSession session,int state,Model model) {
    	int uId = (int) session.getAttribute("UId");
    	PageBean contracts=contractService.findContractByStateAndPage(state, uId, 5, 1);
    	model.addAttribute("contracts", contracts);
    	model.addAttribute("state", state);
    	model.addAttribute("cUId", uId);
    	return "coach_contract.html";
    }
    
    @RequestMapping(value = "/coach_changeContract")
    public String coachChangeContract(int ctId,int state,HttpSession session) {
    	ContractBean contract = new ContractBean();
    	contract.setCtId(ctId);
    	contract.setCtState(state);
    	if(state==4) {
    		Date ctTime = new Date();
    		contract.setCtTime(ctTime);
        	coachService.updateQuality(2, (int)session.getAttribute("UId"));
    	}
    	if(state==5) {
    		if(contractService.findContractByStateAndPage(4, (int)session.getAttribute("UId"), 1, 1).getList()==null) {
        		coachService.updateQuality(1, (int)session.getAttribute("UId"));
    		}
    	}
    	contractService.ChangeContractState(contract);
		return "redirect:/coach_contract?state="+state;
    }
    
    @RequestMapping(value = "/gym_contract")
    public String gymContract(HttpSession session,int state,Model model) {
    	int uId = (int) session.getAttribute("UId");
    	PageBean contracts=contractService.findContractByStateAndPage(state, uId, 5, 1);
    	model.addAttribute("contracts", contracts);
    	model.addAttribute("state", state);
    	model.addAttribute("gUId", uId);
    	return "gym_contract.html";
    }
    
    @RequestMapping(value = "/gym_changeContract")
    public String gymChangeContract(int ctId,int state,HttpSession session) {
    	ContractBean contract = new ContractBean();
    	ContractBean contract1 = contractService.findContractByCtId(ctId);
    	contract.setCtId(ctId);
    	contract.setCtState(state);
    	contractService.ChangeContractState(contract);
    	if(state==4) {
    		Date ctTime = new Date();
    		contract.setCtTime(ctTime);
    		if(contract1.getCtUId1()==session.getAttribute("UId")) {
        		coachService.updateQuality(2, contract1.getCtUId2());
    		}else {
    			coachService.updateQuality(2, contract1.getCtUId1());
    		}
    	}
    	if(state==5) {
    		if(contract1.getCtUId1()==session.getAttribute("UId") && contractService.findContractByStateAndPage(4, contract1.getCtUId2(), 1, 1).getList()==null) {
        		coachService.updateQuality(1, contract1.getCtUId2());
    		}
    		if(contract1.getCtUId2()==session.getAttribute("UId") && contractService.findContractByStateAndPage(4, contract1.getCtUId1(), 1, 1).getList()==null){
    			coachService.updateQuality(1, contract1.getCtUId1());
    		}
    	}
		return "redirect:/gym_contract?state="+state;
    }
}
